package com.hua.yuan.ui;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.ui.data.PictureDataRequest;
import com.hua.yuan.ui.module.ImageDetailBean;
import com.hua.yuan.ui.module.ImageSubItem;
import com.hua.yuan.ui.module.PicBannerBean;
import com.hua.yuan.view.DetailItemView;
import com.hua.yuan.view.ImageBannerView;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/7/15.
 * 显示不同的四个模块
 */

public class ImageShowActivity extends BaseActivity {

    private ImageView backImage;
    private TextView titleView;

    private ImageBannerView myBannerView;

    private DetailItemView detailItemView1;
    private DetailItemView detailItemView2;
    private DetailItemView detailItemView3;

    private int type = 0;
    public static int parentType = 0;

    private List<ImageSubItem> newList;
    private List<ImageSubItem> hotList;
    private List<ImageSubItem> otherList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iamge_show);

        type = getIntent().getIntExtra("type", 0);
        parentType = type;

        initView();
        initData();

    }

    private void initView() {

        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);

        myBannerView = findViewById(R.id.myBannerView);

        detailItemView1 = findViewById(R.id.detailItemView1);
        detailItemView2 = findViewById(R.id.detailItemView2);
        detailItemView3 = findViewById(R.id.detailItemView3);


    }

    @SuppressLint("SetTextI18n")
    private void initData() {

        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        if (type == 0) {
            titleView.setText("火热推荐");
        } else if (type == 1) {
            titleView.setText("付费精品");
        } else if (type == 2) {
            titleView.setText("包月精选");
        } else if (type == 3) {
            titleView.setText("69特价");
        }

        String token = PreferenceUtils.getString(Constents.TOKEN, "");
        PictureDataRequest.getPictureSubRot(token, 1 + type + "", new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("data")) {

                                JSONArray jsonArray = jsonObject.getJSONArray("data");

                                Gson gson = new Gson();
                                List<PicBannerBean> list = new ArrayList<>();

                                for (int i = 0; i < jsonArray.length(); i++) {

                                    JSONObject object = jsonArray.getJSONObject(i);
                                    PicBannerBean bean = gson.fromJson(object.toString(), PicBannerBean.class);
                                    list.add(bean);

                                }

                                if (myBannerView != null) {
                                    myBannerView.setData(list);
                                }

                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

        PictureDataRequest.getPicturesSubIndex(1 + type + "", new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("data")) {
                                JSONObject dataJson = jsonObject.getJSONObject("data");
                                if (!dataJson.isNull("new")) {
                                    //热门
                                    JSONArray newJson = dataJson.getJSONArray("new");

                                    Gson gson = new Gson();

                                    if (newList == null) {
                                        newList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < newJson.length(); i++) {
                                        JSONObject mnew = newJson.getJSONObject(i);
                                        ImageSubItem bean = gson.fromJson(mnew.toString(), ImageSubItem.class);

                                        if (bean != null) {
                                            newList.add(bean);
                                        }
                                    }

                                    if (detailItemView1 != null) {
                                        ImageDetailBean bean = new ImageDetailBean();
                                        bean.setType(0);
                                        bean.setList(newList);
                                        detailItemView1.setData(bean);
                                    }

                                }

                                if (!dataJson.isNull("hot")) {
                                    //付费

                                    JSONArray hotJson = dataJson.getJSONArray("hot");

                                    Gson gson = new Gson();

                                    if (hotList == null) {
                                        hotList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < hotJson.length(); i++) {
                                        JSONObject hot = hotJson.getJSONObject(i);
                                        ImageSubItem bean = gson.fromJson(hot.toString(), ImageSubItem.class);
                                        if (bean != null) {
                                            hotList.add(bean);
                                        }
                                    }

                                    if (detailItemView2 != null) {
                                        ImageDetailBean bean = new ImageDetailBean();
                                        bean.setType(1);
                                        bean.setList(hotList);
                                        detailItemView2.setData(bean);
                                    }

                                }
                                if (!dataJson.isNull("other")) {
                                    //包月

                                    JSONArray otherJson = dataJson.getJSONArray("other");

                                    Gson gson = new Gson();

                                    if (otherList == null) {
                                        otherList = new ArrayList<>();
                                    }

                                    for (int i = 0; i < otherJson.length(); i++) {
                                        JSONObject other = otherJson.getJSONObject(i);
                                        ImageSubItem bean = gson.fromJson(other.toString(), ImageSubItem.class);
                                        if (bean != null) {
                                            otherList.add(bean);
                                        }
                                    }

                                    if (detailItemView3 != null) {
                                        ImageDetailBean bean = new ImageDetailBean();
                                        bean.setType(2);
                                        bean.setList(otherList);
                                        detailItemView3.setData(bean);
                                    }

                                }

                            }

                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }
}
